package com.cheeven.amazonbooksreviewscores;

import java.io.PrintWriter;
import java.util.Map;

public class Top100ReportTemplate {

    public static final String HEADER = "<html>"
            + " <head>"
            + "     <title>Amazon Top 100</title>"
            + "     <style>"
            + "         th, td {padding:3px; font-family:Calibri; font-size:11px; color:black; border:1px solid black;}"
            + "         th {background-color:#999; color:#fff}"
            + "     </style>"
            + " </head>"
            + " <body>"
            + "     <h1>Amazon Top 100</h1>"
            + "     <table border='0' cellpadding='0' cellspacing='0'>"
            + "         <tr>"
            + "             <th colspan='12' rowspan='2'>Basic</th>"
            + "             <th colspan='6' rowspan='2'>Price & Shipping</th>"
            + "             <th colspan='9' rowspan='2'>Review & Discussion</th>"
            + "             <th colspan='8'>Sales Ranking</th>"
            + "             <th colspan='36'>Media Matrix</th>"
            + "         </tr>"
            + "         <tr>"
            + "             <th colspan='2'>Sales Rank</th><th colspan='2'>Sales Rank #1</th><th colspan='2'>Sales Rank #2</th><th colspan='2'>Sales Rank #3</th>"
            + "             <th colspan='5'>Audio Book</th><th colspan='5'>Audio Books Digital</th><th colspan='5'>Hardcover</th><th colspan='5'>Kindle</th><th colspan='5'>Mass Market Paperback</th><th colspan='5'>Other</th><th colspan='5'>Paperback</th>"
            + "         </tr>"
            + "         <tr>"
            + "             <th>Ranking</th><th>Author(s)</th><th>Title</th><th>Book URL</th><th>ASIN</th><th>Merchant ID</th><th>Store ID</th><th>ISBN-10</th><th>ISBN-13</th><th>Language</th><th>Publish Date</th><th>Publisher</th>"
            + "             <th>In Stock</th><th>List Price</th><th>Price</th><th>Shipped By</th><th>Shipping</th><th>You Save</th>"
            + "             <th>1 Star</th><th>2 Star</th><th>3 Star</th><th>4 Star</th><th>5 Star</th><th>Overall</th><th># Reviews</th><th># Discussions</th><th>Discussion URL</th>"
            + "             <th>Category</th><th>Rank</th><th>Category</th><th>Rank</th><th>Category</th><th>Rank</th><th>Category</th><th>Rank</th>"
            + "             <th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th><th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th><th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th><th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th><th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th><th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th><th>Amazon Price</th><th>Format</th><th>New From</th><th>URL</th><th>Used From</th>"
            + "         </tr>";
    public static final String FOOTER = ""
            + "       </table>"
            + " </body>"
            + "</html>";

    static void doBody(Map<String, String> bookInfo, PrintWriter out) {

        out.print("<tr>");
        out.print("<td>" + ns(bookInfo.get("rank")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("basic.author")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("basic.title")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("basic.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("basic.ASIN")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("basic.merchantID")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("basic.storeID")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("content.ISBN-10")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("content.ISBN-13")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("content.Language")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("content.PublishDate")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("content.PublisherName")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("price.inStock")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("price.listPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("price.price")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("price.shippedBy")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("price.shipping")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("price.youSave")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.1star")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.2star")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.3star")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.4star")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.5star")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.average")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("review.total")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("customerDiscussion.count")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("customerDiscussion.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.cat")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.rank")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.sub1.cat")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.sub1.rank")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.sub2.cat")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.sub2.rank")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.sub3.cat")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("salesRank.sub3.rank")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooks.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooks.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooks.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooks.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooks.usedFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooksDigital.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooksDigital.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooksDigital.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooksDigital.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.audiobooksDigital.usedFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.hardcover.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.hardcover.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.hardcover.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.hardcover.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.hardcover.usedFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.kindle.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.kindle.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.kindle.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.kindle.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.kindle.usedFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.massmarketpaperback.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.massmarketpaperback.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.massmarketpaperback.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.massmarketpaperback.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.massmarketpaperback.usedFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.other.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.other.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.other.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.other.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.other.usedFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.paperback.amazonPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.paperback.format")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.paperback.newFromPrice")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.paperback.url")) + "</td>");
        out.print("<td>" + ns(bookInfo.get("twisterMediaMatrix.paperback.usedFromPrice")) + "</td>");
        out.println("</tr>");

    }

    static void logException(PrintWriter out, Throwable e, String rank, String asin, String title, String urlString) {
        out.println("<tr>"
                + "<td>" + rank + "</td>"
                + "<td>" + asin + "</td>"
                + "<td>" + title + "</td>"
                + "<td>" + urlString + "</td>"
                + "<td colspan='100' bgcolor='red'><font color='white'>" + e + "</font></td></tr>");
    }

    static String ns(String s) {
        if (s == null) {
            return "";
        }
        return s;
    }
}
